package cn.vivi.recyclercomp;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

/**
 * 这个Framgent定义了许多的状态，用户可以通过set(status)来切换状态
 * 
 * 每个状态对应一个UI 布局， 用户可以自定义这些布局通过setXX方法（e.g setEmptyView）
 * 
 * @author Vivi Peng
 * 
 */
public abstract class StatusFragment extends Fragment implements IComponentStatus {

	private static final String KEY_ID = "key_id";
	private String mId;

	private RelativeLayout mRootView;
	private StatusHelper mHelper;
	private Status mStatus = Status.EMPTY;


	public StatusFragment() {
	}


	/*
		public static StatusFragment newInstance() {
			StatusFragment frag = new StatusFragment();
			Bundle args = new Bundle();

			frag.setArguments(args);
			return frag;
		}
	*/

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Bundle args = getArguments();
		if (args != null) {
			mId = args.getString(KEY_ID);
		}

	}


	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		mRootView = new RelativeLayout(getActivity());
		mHelper = new StatusHelper(getActivity(), mRootView);

		View newContentView = createDataContentView(inflater, container, savedInstanceState);
		mHelper.setDataContentView(newContentView);
		mHelper.changeStatus(mStatus);
		return mRootView;
	}


	/**
	 * 请确保在onCreateView之后调用
	 * 
	 * @param status
	 */
	public void setStatus(Status status) {
		if (mStatus != status) {
			mStatus = status;
			mHelper.changeStatus(mStatus);
		}
	}


	public Status getStatus() {
		return mStatus;
	}


	public void setDataContentView(View contentView) {
		mHelper.setDataContentView(contentView);
	}


	public void setEmptyView(View emptyView) {
		mHelper.setEmptyView(emptyView);
	}


	public void setEmptyInvalidNetView(View view) {
		mHelper.setEmptyInvalidNetView(view);
	}


	public void setEmptyRefreshing(View view) {
		mHelper.setEmptyRefreshing(view);
	}


	public void setEmptyErrorView(View view) {
		mHelper.setEmptyErrorView(view);
	}


	public View getEmptyView() {
		return mHelper.getEmptyView();
	}


	public View getDataContentView() {
		return mHelper.getDataContentView();
	}


	public View getEmptyRefreshingView() {
		return mHelper.getEmptyRefreshingView();
	}


	public View getEmptyInvalidNetView() {
		return mHelper.getEmptyInvalidNetView();
	}


	public View getEmptyErrorView() {
		return mHelper.getEmptyErrorView();
	}

}
